package net.tv90.bfmccore.game.impl.cq;

import lk.vexview.api.VexViewAPI;
import lk.vexview.hud.VexTextShow;
import net.tv90.bfmccore.util.area.Area;
import net.tv90.bfmccore.util.vector.Vector3i;
import org.bukkit.entity.Player;

import java.util.Collections;
import java.util.List;

public class FrontLineConquestArea extends ConquestArea {

    public FrontLineConquestArea next;
    public FrontLineConquestArea prev;

    public FrontLineConquestArea(Area area, String index, String name, boolean canCapture, List<Vector3i> firstFactionSpawn, List<Vector3i> firstFactionSpawnWhenCapturing, List<Vector3i> secondFactionSpawn, List<Vector3i> secondFactionSpawnWhenCapturing) {
        super(area, index, name, canCapture, firstFactionSpawn, firstFactionSpawnWhenCapturing, secondFactionSpawn, secondFactionSpawnWhenCapturing);
    }

    public static void toLinkedList(FrontLineConquestArea... areas) {
        for (int i = 0; i < areas.length; i++) {
            if (i > 0) {
                areas[i].prev = areas[i - 1];
            } else {
                areas[i].prev = null;
            }

            if (i < areas.length - 1) {
                areas[i].next = areas[i + 1];
            } else {
                areas[i].next = null;
            }
        }
    }

    public static void toLinkedList(List<FrontLineConquestArea> areas) {
        for (int i = 0; i < areas.size(); i++) {
            FrontLineConquestArea area = areas.get(i);
            if (i > 0) {
                area.prev = areas.get(i - 1);
            } else {
                area.prev = null;
            }
            if (i < areas.size() - 1) {
                area.next = areas.get(i + 1);
            } else {
                area.next = null;
            }
        }
    }

    @Override
    public void showProcessBar(Player p, String currentFaction, double processPercent) {
        if (!canCapture(p)) {
            int windowWidth = VexViewAPI.getPlayerClientWindowWidth(p);
            int windowHeight = VexViewAPI.getPlayerClientWindowHeight(p);
            VexTextShow flagText = new VexTextShow("FlagText", 403 * windowWidth / 854, 290 * windowHeight / 445, 100, Collections.singletonList("无法占领"), 1, 1.0);
            VexViewAPI.sendHUD(p, flagText);
            return;
        }
        super.showProcessBar(p, currentFaction, processPercent);
    }

    @Override
    public void firstFactionAddPlayerToArea(Player p) {
        if (canCapture(p)) {
            super.firstFactionAddPlayerToArea(p);
        }
    }

    @Override
    public void secondFactionAddPlayerToArea(Player p) {
        if (canCapture(p)) {
            super.secondFactionAddPlayerToArea(p);
        }
    }

    public void setNext(FrontLineConquestArea next) {
        this.next = next;
    }

    public void setPrev(FrontLineConquestArea prev) {
        this.prev = prev;
    }

    public boolean canCapture(Player p) {
        if (prev != null && next != null) {
            return prev.isOurTeamCaptured(p) || next.isOurTeamCaptured(p);
        } else {
            return true;
        }
    }

}
